import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus, Logger } from '@nestjs/common'
import { Response, Request } from 'express'
import log from 'src/utils/log';
@Catch()
export class AllExceptionFilter implements ExceptionFilter {
	catch(exception: any, host: ArgumentsHost) {
		const context = host.switchToHttp()
		const response = context.getResponse<Response>()
		const req = context.getRequest<Request>()
		const status =
			exception instanceof HttpException
				? exception.getStatus()
				: HttpStatus.INTERNAL_SERVER_ERROR;
		const message = exception instanceof HttpException?exception.getResponse():exception.message

		var logger=log.getLogger('error');
		var logtxt=`${req.method} - ${req.url}\n error:${JSON.stringify(exception,null,2)}`;
		logger.error(logtxt);

		response.status(status).json(message)
	}
}
